package com.dsk.connector.commons.trans;


import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
public class RecordDml implements Serializable {
    /**
     * insert/update/delete
     */
    private String operation;
    /**
     * 表名
     * 格式：database.table
     */
    private String tableName;

    /**
     * 主键字段
     */
    private List<FieldSchema> keySchemas;
    /**
     * 主键值
     */
    private Map<String,Object> keyValue;
    /**
     * payload字段
     */
    private List<FieldSchema> payloadSchemas;
    /**
     * payload值
     */
    private Map<String,Object> payloadValues;

    /**
     * 获取修改的字段，多个字段使用逗号分隔
     * @return
     */
    public String getModifyFields(){
        StringBuilder names= new StringBuilder();
        for(FieldSchema fieldSchema:payloadSchemas){
            names.append(",").append(fieldSchema.getField());
        }
        return names.toString().substring(1);
    }

    public Map<String,FieldSchema> getPayloadSchemaMap(){
        Map<String,FieldSchema> schemaMap=new HashMap<>();
        this.getPayloadSchemas().forEach(fieldSchema -> {
            schemaMap.put(fieldSchema.getField(),fieldSchema);
        });
        return schemaMap;
    }

}
